package xworker.javafx.control;

import javafx.scene.control.TextFormatter;
import javafx.scene.control.TextInputControl;
import javafx.scene.text.Font;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import xworker.javafx.beans.property.PropertyFactory;
import xworker.javafx.util.JavaFXUtils;

public class TextInputControlActions {
    static{
        PropertyFactory.regist(TextInputControl.class, "textFormatterProperty", o -> {
            TextInputControl obj = (TextInputControl) o;
            return obj.textFormatterProperty();
        });
        PropertyFactory.regist(TextInputControl.class, "editableProperty", o -> {
            TextInputControl obj = (TextInputControl) o;
            return obj.editableProperty();
        });
        PropertyFactory.regist(TextInputControl.class, "textProperty", o -> {
            TextInputControl obj = (TextInputControl) o;
            return obj.textProperty();
        });
        PropertyFactory.regist(TextInputControl.class, "promptTextProperty", o -> {
            TextInputControl obj = (TextInputControl) o;
            return obj.promptTextProperty();
        });
        PropertyFactory.regist(TextInputControl.class, "fontProperty", o -> {
            TextInputControl obj = (TextInputControl) o;
            return obj.fontProperty();
        });
    }

    public static void init(TextInputControl node, Thing thing, ActionContext actionContext){
        ControlActions.init(node, thing, actionContext);

        if(thing.valueExists("editable")){
            node.setEditable(thing.getBoolean("editable"));
        }
        if(thing.valueExists("font")){
            Font font = JavaFXUtils.getObject(thing, "font", actionContext);
            if(font != null) {
                node.setFont(font);
            }
        }
        String promptText = JavaFXUtils.getString(thing, "promptText", actionContext);
        if(promptText != null){
            node.setPromptText(promptText);
        }
        if(thing.valueExists("textFormatter")){
            TextFormatter<?> textFormatter = JavaFXUtils.getObject(thing, "textFormatter", actionContext);
            if(textFormatter != null) {
                node.setTextFormatter(textFormatter);
            }
        }
        String text = JavaFXUtils.getString(thing, "text", actionContext);
        if(text != null){
            node.setText(text);
        }
    }
}
